/*
 * Package: org.grassfield.common.dao
 * FileName: UserDAOTest.java
 *  
 * Created by pandian on Jan 28, 2011
 */
package dao;

import java.util.List;

import bean.ContactInfo;
import bean.User;

import test.BaseTest;



/**
 * A junit test case for UserDAO.
 */
public class UserDAOTest extends BaseTest {
	private IUserDAO userDAO;
	private ITeamDAO teamDAO;
	private IUserRoleDAO userRoleDAO;

	/**
	 * Sets the user dao.
	 * 
	 * @param userDAO the new user dao
	 */
	public void setUserDAO(IUserDAO userDAO) {
		this.userDAO = userDAO;
	}

	/**
	 * Sets the team dao.
	 * 
	 * @param teamDAO the new team dao
	 */
	public void setTeamDAO(ITeamDAO teamDAO) {
		this.teamDAO = teamDAO;
	}

	/**
	 * Sets the user role dao.
	 * 
	 * @param userRoleDAO the new user role dao
	 */
	public void setUserRoleDAO(IUserRoleDAO userRoleDAO) {
		this.userRoleDAO = userRoleDAO;
	}

	/**
	 * Test case for list.
	 */
	public void testList() {
		List<User> list = this.userDAO.list();
		super.assertNotNull("list is null", list);
		super.assertTrue("list is empty", list.size()>0);
		System.out.println(list.size()+" user exists");
		for (User user:list){
			System.out.println("User List:"+user.getId()+" "+user.getUserName()+" "+user.getTeam().getName()+" "+user.getContactInfo()+" "+user.getUserRole().getName());
		}
	}

	/**
	 * Test case for get user.
	 */
	public void testGetUser() {
		User user = this.userDAO.getUser("admin", "admin");
		super.assertNotNull("user is null", user);
		super.assertNotNull("user name is null", user.getUserName());
		super.assertNotNull("team is null", user.getTeam());
		super.assertNotNull("contact info is null", user.getContactInfo());
		super.assertNotNull("role is null", user.getUserRole());
		super.assertEquals("admin", user.getUserName());
		System.out.println("User:"+user.getId()+" "+user.getUserName()+" "+user.getTeam()+" "+user.getContactInfo()+" "+user.getUserRole());
		
		user = this.userDAO.getUser("admin", "admin1");
		super.assertNull("user is null", user);
	}
	
	/**
	 * Test case for add user.
	 */
	public void testAddUser(){
		User user = new User();
		user.setUserName("test123");
		user.setTeam(this.teamDAO.find(1L));
		user.setUserRole(this.userRoleDAO.find(1L));
		ContactInfo contactInfo = new ContactInfo();
		contactInfo.setCountry("PAK");
		contactInfo.setEmail1("b@b.com");
		contactInfo.setEmail3("3@3.com");
		user.setContactInfo(contactInfo);
		
		user = this.userDAO.addUser(user);
		super.assertNotNull("id is null", user.getId());
		System.out.println("Added User:"+user.getId()+" "+user.getUserName()+" "+user.getTeam()+" "+user.getContactInfo()+" "+user.getUserRole());
	}
	
}
